package org.mii.safeguard_v1.base.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.mii.safeguard_v1.base.entity.CtProvince;
import org.mii.safeguard_v1.base.entity.CtSP;
import org.mii.safeguard_v1.base.entity.DepRecordReport;
import org.mii.safeguard_v1.security.entity.User;
import org.mii.safeguard_v1.security.service.IUserManager;
import org.mii.safeguard_v1.util.AjaxJQueryUtils;
import org.mii.safeguard_v1.util.JsonBuilderUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.providers.encoding.Md5PasswordEncoder;
import org.springside.modules.web.struts2.Struts2Utils;

/**
 * 用户注册的类。注册分为两步。 <br>
 * 第一步填写基本用户信息，用户登录。第一步数据采用jquery验证。需要prepareModel支撑。 <br>
 * 第二步填写单位信息。第二步数据采用自己js验证吧，已经不能用jquery了……采用struts2默认的bean传递策略。
 * 
 * @author QUANXIWEI
 * @since 2009-07-30
 */
@SuppressWarnings("serial")
@Results( {
		@Result(name = CrudActionSupport.RELOAD, location = "regist.action", type = "redirect"),
		@Result(name = CrudActionSupport.NOTICE, location = "notice.jsp", type = "redirect"),
		@Result(name = CrudActionSupport.DENY_ACCESS, location = "common/403.jsp", type = "redirect") })
public class RegistAction extends CrudActionSupport<User> {

	protected final Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	IUserManager userManager;
	
	
	User entity;// 用户
	Integer id;// 用户id
	DepRecordReport ctDepInfo;// 运营单位信息，考虑安全机构的表是不是不一样呢？
	String userType;// 用户类型
	String deptType;// 运营商类型
	String disType;// 所属地域
	List<CtProvince> ctProList;// 专门为注册界面的区域下拉菜单准备的

	/**
	 * 注册用户的第一步
	 * 
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public String regist1() throws Exception {
		ctProList = userManager
				.findByHql("from CtProvince c where c.disId != '100001' order by c.position asc");
		// return NOTICE;
		return "step1";
	}

	/**
	 * 注册用户的第二步
	 * 
	 * @return
	 * @throws Exception
	 */
	public String regist2() throws Exception {
		Md5PasswordEncoder encoder = new Md5PasswordEncoder();
		System.out.println(encoder.encodePassword("cmdadmin", "admin"));
		System.out.println(userType);
		System.out.println(deptType);
		System.out.println(disType);
		return NOTICE;
		// return "step2";
	}

	/**
	 * 本方法将在regist1方法调用前被调用
	 * 
	 * @throws Exception
	 */
	public void prepareRegist1() throws Exception {
		prepareModel();
	}

	/**
	 * 本方法将在regist2方法调用前被调用
	 * 
	 * @throws Exception
	 */
	public void prepareRegist2() throws Exception {
		prepareModel();
	}

	/**
	 * 检查输入用户是否唯一
	 * 
	 * @return String 'true' or 'false'
	 * @throws Exception
	 */
	public String checkLoginName() throws Exception {
		// HttpServletRequest request = Struts2Utils.getRequest();
		// HttpServletResponse response = Struts2Utils.getResponse();
		// JSONObject jsonObject = AjaxJQueryUtils.readJson(request);
		// String loginName = jsonObject.getString("loginName");
		// response.setCharacterEncoding("UTF-8");
		// if (userManager.isLoginNameUnique(loginName, ""))
		// AjaxJQueryUtils.print(response, "true");
		// else
		// AjaxJQueryUtils.print(response, "false");

		HttpServletRequest request = ServletActionContext.getRequest();
		String loginName = request.getParameter("loginName");
		String oldLoginName = request.getParameter("oldLoginName");

		if (userManager.isLoginNameUnique(loginName, oldLoginName)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}

		return null;
	}

	@SuppressWarnings("unchecked")
	public String deptJson() throws Exception {
		HttpServletRequest request = Struts2Utils.getRequest();
		JSONObject jsonObject = AjaxJQueryUtils.readJson(request);
		int type = jsonObject.getInt("type");

		String op = "=";
		if (type == 0)
			op = ">";
		List<CtSP> deptTypeList = userManager.findByHql(
				"from CtSP c where c.parentid" + op + "?", 0);
		String jsonData = JsonBuilderUtil.getInstance().buildJsonData(
				deptTypeList, "id", "name");
		HttpServletResponse response = Struts2Utils.getResponse();
		response.setCharacterEncoding("UTF-8");
		AjaxJQueryUtils.print(response, jsonData);
		return null;
	}

	public List<CtProvince> getCtProList() {
		return ctProList;
	}

	public void setCtProList(List<CtProvince> ctProList) {
		this.ctProList = ctProList;
	}

	public DepRecordReport getCtDepInfo() {
		return ctDepInfo;
	}

	public void setCtDepInfo(DepRecordReport ctDepInfo) {
		this.ctDepInfo = ctDepInfo;
	}

	@Override
	public String delete() throws Exception {
		return null;
	}

	@Override
	public String list() throws Exception {
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = userManager.get(id);
		} else {
			entity = new User();
		}
	}

	@Override
	public String save() throws Exception {
		return null;
	}

	public User getModel() {
		return entity;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public String getDeptType() {
		return deptType;
	}

	public void setDeptType(String deptType) {
		this.deptType = deptType;
	}

	public String getDisType() {
		return disType;
	}

	public void setDisType(String disType) {
		this.disType = disType;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

}
